<?php


namespace app\index\controller;


use app\index\model\Paypal;
use think\Exception;
use think\Request;

class PaypalApi extends BaseController
{
    /**
     * @var bool
     */
    private static $ssl_verify=false;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $domain=$request->domain();
        if(strpos($domain, 'https') === 0){
            self::$ssl_verify=true;
        }
    }

    private static $token_auth_user='';
    private static $token_auth_pass='';
    private static $http=PAYPAL_HTTP;
    private static $token_url='/v1/oauth2/token';
    private static $create_url='/v2/invoicing/invoices';
    private static $curl='/v1/shipping/';
    private static $token=null;
    private static $invoicer=[];

    public static function getToken($group){
        if(self::$token){
            return ['status'=>true,'result'=>['token'=>self::$token]];
        }
        try {
            ['status'=>$status,'result'=>$result]= Paypal::getPaypalInfo($group);
            if($status===false){
                return ['status'=>false,'result'=>$result];
            }
            ['token'=>$token_arr,'invoicer'=>$invoicer,'secret_user'=>$secret_user] =$result;
            self::$invoicer=$invoicer;
            self::$token_auth_user=$secret_user['client_id'];
            self::$token_auth_pass=$secret_user['secret'];
        } catch (Exception $e) {
            trace('get token error'.$e->getMessage(),'error');
            return ['status'=>false,'result'=>'读取配置失败'];
        }
        $token=$token_arr['token'];
        $expires_in=$token_arr['expires_in'];
        $last_get_token=$token_arr['last_get_time'];
        $expires_time=time()-strtotime($last_get_token);
        if(($expires_in-$expires_time)<60*30){
            ['status'=>$status,'result'=>$result]=self::getNewToken();
            if($status===false){
                return ['status'=>false,'result'=>$result];
            }
            $data = Paypal::get($token_arr['id']);
            if($data === null){
                return ['status'=>false,'result'=>'更新数据失败'];
            }
            $data->token=$result['token'];
            $data->expires_in=$result['expires_in'];
            $data->last_get_time=date('Y-m-d H:i:s');
            $data->save();
            self::$token=$result['token'];
            return ['status'=>true,'result'=>['token'=>$result['token']]];
        }
        self::$token=$token;
        return ['status'=>true,'result'=>['token'=>$token]];
    }

    public static function getNewToken(){
        //https://api.sandbox.paypal.com/v1/oauth2/token
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.self::$token_url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => 'grant_type=client_credentials',
            CURLOPT_HTTPHEADER => array(
                'Authorization: Basic '.base64_encode(self::$token_auth_user.':'.self::$token_auth_pass).'',
                'Content-Type: application/x-www-form-urlencoded'
            ),
        ));
        $response = curl_exec($curl);
        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
//            throw new RuntimeException('curl error'.$error);
            return self::curlError($error);
        }
        $response = json_decode($response, true);

        if ($http_code===200) {
            return ['status' => true, 'result'=>['token' => $response['access_token'], 'expires_in' => $response['expires_in']]];
        }
        ['status'=>$status,'result'=>$result]=self::errorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }

    /**
     * invoiceParam
     * 获取账单信息数组
     * @url
     * @method
     * @param array $data
     * @return array $param
     * @author Asira
     * @date 2020/12/28
     */
    public static function invoiceParam($data): array
    {

        $data['tip'] = $data['tip'] === 1;
        $data['curr']='USD';
        $data['term_type']='DUE_ON_DATE_SPECIFIED';
        $data['due_date']=date( 'Y-m-d');

        $partial_payment=[
            "allow_partial_payment" => $data['payment_type']===0,
        ];
        if($data['payment_type']===0){
            $partial_payment['minimum_amount_due']=[
                "currency_code" =>$data['curr'],
                "value" => $data['partial_pay']
            ];
        }
        $items=[];
        foreach ($data['items'] as $value){
            if($value['gift'] === 1) {
                continue;
            }elseif($value['gift']===2){
                $value['en_desc'] = $value['en_name'] . '(' . $value['amount'] . 'pc)(gift)';
            }else{
                if($value['type']==1){
                    $value['en_desc']=$value['en_name'].'('.$value['amount'].'PC)';
                }elseif($value['type']==2){
                    $value['en_desc']=$value['en_name'].'('.$value['amount'].'PC)';
                }else{
                    $value['en_desc']=$value['en_name'].'('.$value['amount'].'PC)';
                }
            }

            $items[]=[
                "name" => '',
                "description" => $value['en_desc'],
                "quantity" => 1,
                "unit_amount" => [
                    "currency_code" => $data['curr'],
                    "value" => $value['selling_price']
                ],
                "unit_of_measure" => "QUANTITY"
            ];
        }
        $params = [
            "status"=>"DRAFT",
            "detail" => [
                "invoice_number" => $data['order_id'],
                "reference" => $data['order_id'],
                "invoice_date" => $data['create_time'],
                "currency_code" =>$data['curr'],
                "note" => "Open box means there is no shipping fee included and when you pay for these items they will be put in the open-paid section, as you continue shopping.If you wish for us to ship your items right away, let us know before paying and we add the shipping fee, and if you are still shopping please let us know when you will be ready to ship and we add the shipping cost on the final invoice. Thank you.",
                "term" => "",
                "memo" => "",
                "payment_term" => [
                    "term_type" => $data['term_type'],
                    "due_date" => $data['due_date']
                ]
            ],
            "invoicer" =>self::$invoicer,
            "primary_recipients" => [
                [
                    "billing_info" => [
                        "name" => [
                            "given_name" => $data['user_name'],
                            "surname" => ""
                        ],
                        "address" => [
                            "address_line_1" => $data['address'],
                            "admin_area_2" => $data['city'],
                            "admin_area_1" => $data['city'],
                            "postal_code" => $data['zip_code'],
                            "country_code" => $data['country_code']
                        ],
                        "email_address" => $data['email'],
                    ],
                    "shipping_info" => [
                        "name" => [
                            "given_name" => $data['user_name'],
                            "surname" => ""
                        ],
                        "address" => [
                            "address_line_1" => $data['address'],
                            "admin_area_2" => $data['city'],
                            "admin_area_1" => $data['city'],
                            "postal_code" => $data['zip_code'],
                            "country_code" => $data['country_code']
                        ]
                    ]
                ]
            ],
            "items" => $items,
            "configuration" => [
                "partial_payment" => $partial_payment,
                "allow_tip" => $data['tip'],
            ],
            "amount" => [
                "breakdown" => [
                    "shipping" => [
                        "amount" => [
                            "currency_code" => $data['curr'],
                            "value" => $data['ship_fee']
                        ],
                    ],
                    "discount" => [
                        "invoice_discount" => [
//                            "percent"=>''%
                            "amount" => [
                                "currency_code" => $data['curr'],
                                "value" => $data['amount_discount']
                            ]
                        ]
                    ]
                ],
            ]
        ];
        if($data['cc_email']) {
            $cc_email = explode(',', $data['cc_email']);
        }else{
            $cc_email=[];
        }
        if(!empty($cc_email)) {
            $params["additional_recipients"] = $cc_email;
        }
        return $params;
    }

    public static function curlError($error): array
    {
        trace('pay pal curl error '.$error,'error');
        return ['status'=>false,'result'=>'curl error'.$error];
    }

    public static function createInvoice($data){
        ['status'=>$status,'result'=>$result]=self::getToken($data['create_group']);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $token=$result['token'];
        $params=self::invoiceParam($data);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.self::$create_url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>json_encode($params),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);
        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
//            throw new RuntimeException('curl error'.$error);
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===201){
            return self::getInvoiceDetail($response['href']);
        }
        ['status'=>$status,'result'=>$result]=self::errorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }

    public static function getInvoiceDetail($url){
        ['status'=>$status,'result'=>$result]=self::getToken(0);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $token=$result['token'];
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'GET',
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token
            ),
        ));

        $response = curl_exec($curl);
        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
//            throw new RuntimeException('curl error'.$error);
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===200){
            return ['status'=>true,'result'=>$response];
        }
        ['status'=>$status,'result'=>$result]=self::errorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }

    public static function errorStatus($http_code,$response){
        trace('Exception Error.
        Paypal API'.json_encode($response),'error');
        if($http_code===401) {
            return ['status' => false, 'result' => ['msg'=>$response['error'] . ' ' . $response['error_description']]];
        }
        if(in_array($http_code, [400, 405, 422], true)){
            return ['status' => false, 'result' => ['msg'=>$response['details'][0]['description']??$response['message'],'issue'=>$response['details'][0]['issue']]];
        }
        return ['status'=>false,'result'=>['msg'=>'unknown error status'.$http_code]];
    }

    public static function updateInvoice($data){
        ['status'=>$status,'result'=>$result]=self::getToken($data['create_group']);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $token=$result['token'];
        $params=self::invoiceParam($data);

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.self::$create_url.'/'.$data['receipt_number'],
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'PUT',
            CURLOPT_POSTFIELDS =>json_encode($params),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);
        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
//            throw new RuntimeException('curl error'.$error);
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===200){
            return self::getInvoiceDetail($response['href']);
        }
        ['status'=>$status,'result'=>$result]=self::errorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }

    public static function sentInvoice($invoice_id,$group){
        ['status'=>$status,'result'=>$result]=self::getToken($group);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $token=$result['token'];
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.self::$create_url.'/'.$invoice_id.'/send',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>'{"send_to_invoicer": true}',
            CURLOPT_HTTPHEADER => array(
                'PayPal-Request-Id: b1d1f06c7246c',
                'Authorization: Bearer '.$token,
                'Content-Type: application/json',
            ),
        ));

        $response = curl_exec($curl);
        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
//            throw new RuntimeException('curl error'.$error);
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===200){
            return ['status'=>true,'result'=>'发送账单成功'];
        }
        ['status'=>$status,'result'=>$result]=self::errorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }

    public static function cancelInvoice($invoice_id,$group): array
    {
        ['status'=>$status,'result'=>$result]=self::getToken($group);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $token=$result['token'];
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.self::$create_url.'/'.$invoice_id.'/cancel',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS => '{}',
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);

        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===204){
            return ['status'=>true,'result'=>'取消账单成功'];
        }
        ['status'=>$status,'result'=>$result]=self::errorStatus($http_code,$response);
        if(isset($result['issue']) && $result['issue']==='INVOICE_CANCELED_ALREADY'){
            return ['status'=>true,'result'=>'取消账单成功'];
        }
        return ['status'=>$status,'result'=>$result['msg']];
    }

    public static function deleteInvoice($invoice_id,$group){
        ['status'=>$status,'result'=>$result]=self::getToken($group);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $token=$result['token'];
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL =>  self::$http.self::$create_url.'/'.$invoice_id,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'DELETE',
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token
            ),
        ));

        $response = curl_exec($curl);

        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===204){
            return ['status'=>true,'result'=>'删除账单成功'];
        }
        ['status'=>$status,'result'=>$result]=self::errorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }

    /**
     * recordPayment
     * 更新pay pal付款记录
     * @url
     * @method
     * @param string $invoice_id 账单ID
     * @param array $data 付款数据
     * @return array
     * @author Asira
     * @date 2021/1/4
     */
    public static function recordPayment($invoice_id,$group, $data): array
    {
        ['status'=>$status,'result'=>$result]=self::getToken($group);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $token=$result['token'];

        $params=[
            'method'=>$data['method'],
            'payment_date'=>explode(' ',$data['payment_date'])[0],
            'note'=>$data['note'],
            'amount'=>[
                'currency_code'=>'USD',
                'value'=>$data['amount'],
            ],
        ];

        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.self::$create_url.'/'.$invoice_id.'/payments',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>json_encode($params),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);

        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===200){
            return ['status'=>true,'result'=>['msg'=>'更新付款成功','payment_id'=>$response['payment_id']]];
        }
        ['status'=>$status,'result'=>$result]=self::errorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }

    public static function addWaybillCode($trackers,$group): array
    {
        ['status'=>$status,'result'=>$result]=self::getToken($group);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $data['trackers']=array_values($trackers);
        $token=$result['token'];
        $curl = curl_init();
//        echo json_encode($trackers);
        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.self::$curl.'trackers-batch',
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'POST',
            CURLOPT_POSTFIELDS =>json_encode($data),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);

        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===200){
            if (!empty($response['errors'])){
                return ['status' => false, 'result' => $response['errors'][0]['message']];
            }
            if (!empty($response['message'])){
                return ['status' => false, 'result' => $response['message']];
            }
            return ['status'=>true,'result'=>'添加运单号成功'];
        }
        ['status'=>$status,'result'=>$result]=self::getErrorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }

    public static function getErrorStatus($http_code,$response){
        trace('Exception Error.
        Paypal API'.json_encode($response),'error');
        if($http_code===401) {
            return ['status' => false, 'result' => ['msg'=>$response['error'] . ' ' . $response['error_description']]];
        }
        if($http_code===404) {
            return ['status' => false, 'result' => ['msg'=>$response['message']]];
        }
        if(in_array($http_code, [400, 405, 422], true)){
            return ['status' => false, 'result' => ['msg'=>$response['errors'][0]['details'][0]['description']??$response['message'],'issue'=>$response['errors'][0]['details'][0]['issue']]];
        }
        return ['status'=>false,'result'=>['msg'=>'unknown error status'.$http_code]];
    }
    public static function updateWaybillCode($trackers,$group,$pre_tracking_number)
    {
        ['status'=>$status,'result'=>$result]=self::getToken($group);
        if($status===false){
            return ['status'=>false,'result'=>$result];
        }
        $str=$trackers['transaction_id'].'-'.$pre_tracking_number;
        $token=$result['token'];
        $curl = curl_init();

        curl_setopt_array($curl, array(
            CURLOPT_URL => self::$http.self::$curl.'trackers/'.$str,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_ENCODING => '',
            CURLOPT_MAXREDIRS => 10,
            CURLOPT_TIMEOUT => 0,
            CURLOPT_SSL_VERIFYPEER=>self::$ssl_verify,
            CURLOPT_FOLLOWLOCATION => true,
            CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
            CURLOPT_CUSTOMREQUEST => 'PUT',
            CURLOPT_POSTFIELDS =>json_encode($trackers),
            CURLOPT_HTTPHEADER => array(
                'Authorization: Bearer '.$token,
                'Content-Type: application/json'
            ),
        ));

        $response = curl_exec($curl);

        $error=curl_error($curl);
        $http_code=curl_getinfo($curl,CURLINFO_HTTP_CODE);
        curl_close($curl);
        if($error){
            return self::curlError($error);
        }
        $response=json_decode($response,true);
        if($http_code===204){
            return ['status'=>true,'result'=>'更新运单号成功'];
        }
        ['status'=>$status,'result'=>$result]=self::getUpdateErrorStatus($http_code,$response);
        return ['status'=>$status,'result'=>$result['msg']];
    }
    public static function getUpdateErrorStatus($http_code,$response){
        trace('Exception Error.
        Paypal API'.json_encode($response),'error');
        if($http_code===401) {
            return ['status' => false, 'result' => ['msg'=>$response['error']]];
        }
        if($http_code===404||$http_code===400) {
            return ['status' => false, 'result' => ['msg'=>$response['message']]];
        }
        return ['status'=>false,'result'=>['msg'=>'unknown error status'.$http_code]];
    }
}